﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using WinFormsGraphicsDevice;
using Microsoft.Xna.Framework.Graphics;
using TinyEngine.Editor.Extensibility;

namespace TinyEngineEditor.SceneViewer
{
    public partial class SceneViewer : UserControl
    {
        public static SceneViewer Instance;
        public SceneViewer()
        {
            Instance = this;
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                deviceControl = new SceneViewerGraphicsControl();
                deviceControl.ControlCreated += new Action(deviceControl_ControlCreated);
                this.windowsFormsHost1.Child = deviceControl;
                EditorSession.SceneRefreshRequested += new Action(EditorSession_SceneRefreshRequested);
                EditorSession.Instance.SceneClosed += new Action(Instance_SceneClosed);
            }    
        }

        void Instance_SceneClosed()
        {
            this.deviceControl.Reset();
        }

        void EditorSession_SceneRefreshRequested()
        {
            this.deviceControl.Invalidate();
        }
        public void FinishedInit()
        {
            EditorSession.Instance.MoveSceneRequest += new Action<Vector3>(Instance_MoveSceneRequest);
        }

        void deviceControl_ControlCreated()
        {
            if (FinishedLoading != null)
                FinishedLoading();
        }

        void Instance_MoveSceneRequest(Vector3 obj)
        {
            deviceControl.MoveCamera(obj);
        }

        public event Action FinishedLoading;
        SceneViewerGraphicsControl deviceControl = null;

        public static ServiceContainer Services
        {
            get { return Instance.deviceControl.Services; }
        }
        
        public static GraphicsDevice Graphics
        {
            get { return Instance.deviceControl.GraphicsDevice; }
        }

        internal void ForceRedraw()
        {
            this.deviceControl.Invalidate();
        }

        private void sliderZoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (this.deviceControl == null) return;
            this.deviceControl.SetZoom((float)sliderZoom.Value);
        }

        bool isCaptured;
        private void sliderX_LostMouseCapture(object sender, MouseEventArgs e)
        {
            isCaptured = false;
            ((Slider)sender).Value = 0;
        }

        private void sliderY_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (isCaptured)
            {
                this.deviceControl.MoveCamera(new Vector3(0, 100 * (float)sliderY.Value, 0));
            }
        }
        private void sliderX_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (isCaptured)
            {
                this.deviceControl.MoveCamera(new Vector3(100 * (float)sliderX.Value, 0, 0));
            }
        }

        private void sliderY_GotMouseCapture(object sender, MouseEventArgs e)
        {
            isCaptured = true;
        }

    }

 
}
